<?php

class PublishController extends AppController {
	var $name = 'Publish';
	var $uses = array();
	
	function admin_act($id = null){
		if(empty($id)){
			$this->Session->setFlash('Param error.');
			return ;
		}
		$this->autoRender = false;
		App::import('Component', 'HtmlGenerate');
		$htmlgenerate = new HtmlGenerateComponent();
		$static       = $htmlgenerate->generate(null, 'http://'.$_SERVER['SERVER_NAME'].'/articles/view/'.$id, $id);
		if($static['rlt']){
			$this->loadModel('Article');
			$this->Article->read(null, $id);
			$this->Article->set(array('html'=> 'htmls/'.$static['msg']));
			if($this->Article->save()){
				$this->Session->setFlash('Generate html success.');
			} else {
				$this->Session->setFlash('save html faild.');
			}
		} else {
			$this->Session->setFlash($static['msg']);
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	
	function admin_unpub($id = null){
		if(empty($id)){
			$this->Session->setFlash('Param error.');
			return ;
		}
		$this->autoRender = false;
		$this->loadModel('Article');
		$this->Article->read(null, $id);
		$this->Article->set(array('html'=> NULL));
		if($this->Article->save()){
			$this->Session->setFlash('Unpublish html success.');
		} else {
			$this->Session->setFlash('Unpublish html faild.');
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
	function admin_ipage(){
		App::import('Component', 'HtmlGenerate');
		$htmlgenerate = new HtmlGenerateComponent();
		$static       = $htmlgenerate->generate('index', 'http://'.$_SERVER['SERVER_NAME'].'/index.php');
		if($static['rlt']){
			$this->Session->setFlash('Generate html success.');
		} else {
			$this->Session->setFlash($static['msg']);
		}
		$this->redirect($_SERVER['HTTP_REFERER']);
	}
}